﻿using System;
using BikeBouncer.BusinessLayer;
using BikeBouncer.BusinessLayer.Entities;
using BikeBouncer.Site.Enums;
using Devv.Core.Utils;

namespace BikeBouncer.Site
{
	public partial class MyDetails_aspx : BasePage
	{
		#region Page methods

		protected void Page_Load(Object sender, EventArgs e)
		{
			SessionManager.NeedsUserLogin();

			if (!Page.IsPostBack)
			{
				FirstLoad();
			}
		}

		private void FirstLoad()
		{
			RegisterScript(ScriptSource.ToolTips);
			RegisterScript(ScriptSource.ColorBox);

			AjaxManager.SetDefaults_AutoCompleteExtender(AutoComplete_TxtCity);

			PanSaved.Visible = false;

			ValTxtEmail.ValidationExpression = RegexUtil.Email;
			TxtEmail.Text = SessionManager.LoggedUser.Email;
			TxtDisplayName.Text = SessionManager.LoggedUser.DisplayName;
			TxtPostcode.Text = SessionManager.LoggedUser.Address.Postcode;
			TxtLocation.Text = SessionManager.LoggedUser.Address.Location;
			TxtCity.Text = SessionManager.LoggedUser.Address.City.ToString();
		}

		#endregion

		#region Register

		protected void ButSave_Click(Object sender, EventArgs e)
		{
			Page.Validate();

			if (Page.IsValid)
			{
				TxtEmail.Text = TxtEmail.Text.Trim();

				if (TxtPassword.Text.Length > 1)
				{
					TxtPassword.Text = TxtPassword.Text.Trim();
				}

				if (TxtDisplayName.Text.Length > 1)
				{
					TxtDisplayName.Text = TxtDisplayName.Text.Trim();
				}

				if (TxtPostcode.Text.Length > 1)
				{
					TxtPostcode.Text = TxtPostcode.Text.Trim();
				}

				if (TxtLocation.Text.Length > 1)
				{
					TxtLocation.Text = TxtLocation.Text.Trim();
				}

				if (TxtCity.Text.Length > 1)
				{
					TxtCity.Text = TxtCity.Text.Trim();
				}
				
				UpdateAccount();
			}
			else
			{
				AppendError(GetGlobalString("PageNotValid"));
			}
		}

		private void UpdateAccount()
		{
			User user = SessionManager.LoggedUser;

			user.Email = TxtEmail.Text;
			user.Password = TxtPassword.Text; 
			user.DisplayName = TxtDisplayName.Text;
			
			Address address;

			if (!String.IsNullOrEmpty(TxtPostcode.Text) || !String.IsNullOrEmpty(TxtLocation.Text) || !String.IsNullOrEmpty(TxtCity.Text))
			{
				address = RegisterAddress();
			}
			else
			{
				address = new Address();
			}

			user.Address = address;

			try
			{
				DataAccess.User_Upsert(user);

				if (!String.IsNullOrEmpty(user.Password))
				{
					DataAccess.User_UpdatePassword(user);
				}
				
				PanSaved.Visible = true;
			}
			catch (Exception ex)
			{
				ErrorManager.Log(ex, "Error trying to update user details.");
				AppendError(ex, "User");

				PanSaved.Visible = false;
			}
		}

		private Address RegisterAddress()
		{
			try
			{
				Address address = new Address();

				address.UserId = SessionManager.LoggedUser.Id;
				address.Postcode = TxtPostcode.Text;
				address.Location = TxtLocation.Text;
				address.Active = true;

				address.City = City.Fetch(TxtCity.Text);

				if (String.IsNullOrEmpty(address.City.Country))
				{
					address.City.Country = SessionManager.CurrentCountry;
				}
				
				DataAccess.Address_Upsert(address);

				return address;
			}
			catch (Exception ex)
			{
				ErrorManager.Log(ex, "Error trying to register user address.");
				AppendError(ex, "Address");

				return null;
			}
		}

		#endregion
	}
}